package com.feibi.firstplugin.Action;

import com.feibi.firstplugin.Bean.WordData;
import com.feibi.firstplugin.Utils.NotifyUtil;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileVisitor;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class WaitDialog extends DialogWrapper {
    Project project;
    VirtualFile virtualFile;

    final static int TRANSFER_ING = 1;
    final static int TRANSFER_DONE = 2;
    int status = 0;
    List<WordData.Result> wordResults;


    public WaitDialog(@Nullable Project project, List<WordData.Result> wordResults, VirtualFile virtualFile) {
        super(project);
        setTitle("转繁体 —— Design By 亦心");
        init();
        this.wordResults = wordResults;
        this.project = project;
        this.virtualFile = virtualFile;
        status = TRANSFER_ING;
        getWindow().addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                getAllVF();
            }
        });

    }


    @Override
    protected @Nullable
    JComponent createCenterPanel() {
        JPanel jPanel = new JPanel();
        Dimension dimension = new Dimension();
        dimension.setSize(400, 200);
        jPanel.setPreferredSize(dimension);
        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
        jPanel.add(Box.createVerticalGlue());
        label.setAlignmentX(0.5f);
        label.setFont(new Font("Serif", Font.PLAIN, 30));
        jPanel.add(label);
        jPanel.add(Box.createVerticalGlue());
        return jPanel;
    }

    JLabel label = new JLabel("Waiting...");
    JButton button = new JButton("Waiting");

    @Override
    protected @Nullable
    JComponent createSouthPanel() {
        JPanel jPanel = new JPanel();
        button.addActionListener(e -> {
            if (status == TRANSFER_DONE) {
                dispose();
            }
        });
        jPanel.add(button);
        return jPanel;
    }

    int time = 0;
    java.util.Timer timer = new Timer();

    void startTimer() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                time += 100;
                if (time > 1500) {
                    status = TRANSFER_DONE;
                    button.setText("关闭");
                    label.setText("转换完成");
//                    NotifyUtil.notify("转换完成！");
                    System.out.println("-----------------------任務完成-------------------------");
                    timer.cancel();
                }
            }
        }, 0, 100);
    }

    void getAllVF() {
        System.out.println("-----------------------开始任务-------------------------");
        startTimer();
        VirtualFileVisitor visitor = new VirtualFileVisitor() {
            @Override
            public boolean visitFile(VirtualFile file) {
                time = 0;
                replaceFile(file);
                return true;
            }
        };

        if (virtualFile.isDirectory()) {
            // 遍历目录下的所有子目录和文件，包括隐藏文件
            VfsUtil.visitChildrenRecursively(virtualFile, visitor);
        } else {
            replaceFile(virtualFile);
            time = 1500;
        }
    }


    void replaceFile(VirtualFile file) {
        Document document = FileDocumentManager.getInstance().getDocument(file);
        if (document != null) {
            if (document.isWritable()) {
                WriteCommandAction.runWriteCommandAction(project, () -> {
                    String s = document.getText();
                    for (int i = 0; i < wordResults.size(); i++) {
                        s = s.replaceAll(wordResults.get(i).getCnName(), wordResults.get(i).getTwName());
                    }
                    document.setText(s);
                });
            }
        }
    }


}
